﻿using MedicalManagementAPP.AccountModule;
using MedicalManagementAPP.MyException;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MedicalManagementAPP
{
    static class AccountManager
    {
        const int maxInt = 9;
        /// <summary>
        /// 通过输入的参数尝试登陆，如果成功则返回一个Person对象，否则抛出PersonNotFound或者WrongPassword
        /// </summary>
        /// <returns>登陆的账号对应的Person对象</returns>
        public static Person Login(string name, string psw, string type)
        {
            // 进行SQL查询
            // type 最终会变成"Patient" "Worker" 或者 "Admin"
            string sql;
            switch (type)
            {
                case "患者":
                    if (name.Length > maxInt)
                    {
                        sql = "select PatientName,Password,PatientID from Patients where Phone=@name";
                    }
                    else
                    {
                        sql = "select PatientName,Password,PatientID from Patients where PatientID=@name";
                    }
                    type = "Patient";
                    break;
                case "医生":
                    if(name.Length > maxInt)
                    {
                        sql = "select WorkerName,Password,WorkerID from Workers where Phone=@name and WorkerType<>'管理员'";
                    }
                    else
                    {
                        sql = "select WorkerName,Password,WorkerID from Workers where WorkerID=@name and WorkerType<>'管理员'";
                    }
                    type = "Worker";
                    break;
                case "管理员":
                    if (name.Length > maxInt)
                    {
                        sql = "select WorkerName,Password,WorkerID from Workers where Phone=@name and WorkerType='管理员'";
                    }
                    else
                    {
                        sql = "select WorkerName,Password,WorkerID from Workers where WorkerID=@name and WorkerType='管理员'";
                    }
                    type = "Admin";
                    break;
                default:
                    sql = null;
                    break;
            }
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@name",name),
            };
            DataTable data = Connection.SqlQuery(sql,parameters);
            // 账号是否存在判断
            if (data.Rows.Count != 1)
            {
                throw new PersonNotFound(name);
            }
            // 密码是否符合判断
            if(data.Rows[0][1].ToString() != psw)
            {
                throw new WrongPassword(name);
            }
            string id = data.Rows[0][2].ToString();
            Person person = new Person(data.Rows[0][0].ToString(), type, id);
            return person;
        }
        public static bool PatientReg(string name, string psw, string phone) {
            string sql;
            SqlParameter[] parameters;
            string date = DateTime.Now.ToString();

            // 检测用户是否存在
            sql = "select PatientName,Password from Patients where Phone='@name'";
            parameters = new SqlParameter[] {
                // 手机号是用户除ID外唯一凭证
                new SqlParameter("@name",phone)
            };
            DataTable data = Connection.SqlQuery(sql, parameters);
            if(data.Rows.Count != 0)
            {
                throw new PersonExist(phone);
            }
            if (phone.Length < 9)
            {
                throw new WrongPhone(name);
            }
            // 将注册数据输入数据库
            sql = "insert into Patients(PatientName,SignDate,Password,Phone) values(@name,@date,@psw,@phone)";
            parameters = new SqlParameter[]
            {
                new SqlParameter("@name",name),
                new SqlParameter("@date",date),
                new SqlParameter("@psw",psw),
                new SqlParameter("@phone",phone)
            };
            if(Connection.SqlNoQuery(sql, parameters)){
                return true;
            }
            else
            {
                return false;
            }
            
        }
        /// <summary>
        /// 得到当前用户的信息，顺序是WorkerName,Password,DepartmentName,Phone,WorkerType
        /// </summary>
        /// <returns>顺序为WorkerName,Password,DepartmentName,Phone,WorkerType的DataTable</returns>
        public static DataRow GetWorkerInfo(string id)
        {
            string sql = "select WorkerName,Password,DepartmentName,Phone,WorkerType from Workers W,Department D where WorkerID=@id and W.DepartmentID=D.DepartmentID";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            return Connection.SqlQuery(sql, parameters).Rows[0];
        }
        /// <summary>
        /// 得到当前用户的信息，顺序是PatientID,PatientName,SignDate,Password,Phone
        /// </summary>
        /// <returns>顺序是PatientID,PatientName,SignDate,Password,Phone</returns>
        public static DataRow GetPatientInfo(string id)
        {
            string sql = "select PatientID,PatientName,SignDate,Password,Phone from Patients where PatientID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            return Connection.SqlQuery(sql, parameters).Rows[0];
        }
    }
}
